package com.itboye.textileexpres.collection;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.ColletionFenyeBeans;
import com.itboye.textileexpes.presenter.ColloctionPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.collection.ActivityCollectionEdit.MyListViewAdapter;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;

public class ActivityIsOrder extends BaseActivity implements OnClickListener,
		Observer, TextWatcher {
	static ColletionFenyeBeans beans;
	LinearLayout lineyout;
	Button btnSendOrder;
	private ImageView back, img_other;
	ColloctionPresenter presenter;
	private TextView title;
	private EditText editextNote, tvgetInfo;
	List<String> ids = new ArrayList<String>();
	MyListViewAdapter listViewAdapter;
	String strIds;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_orderisorder;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		img_other.setVisibility(View.GONE);
		presenter = new ColloctionPresenter(this);
		title.setText(getString(R.string.h105));
		beans = (ColletionFenyeBeans) getIntent().getSerializableExtra("book");
		strIds = getIntent().getStringExtra("ids");
		System.out.println("strIDs" + strIds);
		tvgetInfo.addTextChangedListener(this);
		editextNote.addTextChangedListener(this);
		if (beans.getList().size() > 0) {

			for (int i = 0; i < beans.getList().size(); i++) {
				if (beans.getList().get(i).isCheck == true) {
					System.out.println("istrue"
							+ beans.getList().get(i).isCheck);
					View view = LayoutInflater.from(ActivityIsOrder.this)
							.inflate(R.layout.add_coller_order, null);
					TextView goods_name1_tv = (TextView) view
							.findViewById(R.id.goods_name1_tv);
					TextView goods_name2_tv = (TextView) view
							.findViewById(R.id.goods_name2_tv);
					TextView goods_detail_tv = (TextView) view
							.findViewById(R.id.goods_detail_tv);

					TextView publish_author_tv = (TextView) view
							.findViewById(R.id.publish_author_tv);

					TextView area_tv = (TextView) view
							.findViewById(R.id.area_tv);

					ImageView goods_img = (ImageView) view
							.findViewById(R.id.goods_img);
					goods_name1_tv.setText(beans.getList().get(i)
							.getFavorite_content().getProduct_code());

					goods_name2_tv.setText(beans.getList().get(i)
							.getFavorite_content().getName());
					goods_detail_tv.setText(beans.getList().get(i)
							.getFavorite_content().getSynopsis());

					publish_author_tv.setText(getString(R.string.sender)
							+ ":"
							+ beans.getList().get(i).getFavorite_content()
									.getContact_name());
					if (beans.getList().get(i).getFavorite_content()
							.getPlace_origin() == null
							|| beans.getList().get(i).getFavorite_content()
									.getPlace_origin() == "") {
						area_tv.setText("");
					} else {
						area_tv.setText(getString(R.string.placeOrigin)
								+ ":"
								+ beans.getList().get(i).getFavorite_content()
										.getPlace_origin());
					}
					XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
							+ beans.getList().get(i).getFavorite_content()
									.getId(), goods_img);
					MarginLayoutParams layoutParams = new MarginLayoutParams(
							MarginLayoutParams.WRAP_CONTENT,
							MarginLayoutParams.WRAP_CONTENT);
					layoutParams.setMargins(5, 5, 5, 5);
					Log.e("creaID",
							TimeFormat.DateFormat(beans.getList().get(i)
									.getCreate_time()));
					lineyout.addView(view, layoutParams);

				}
			}
		}

	}

	public String onIsID() {
		String s = null;
		for (int i = 0; i < beans.getList().size(); i++) {
			s = beans.getList().get(i).getFavorite_id();
			ids.add(s);
		}
		return s;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnSendOrder:
			beans = new ColletionFenyeBeans();
			if (tvgetInfo.getText().toString().equals("")
					|| tvgetInfo.getText().toString() == null) {
				toast(getString(R.string.h106));
			}
			presenter.oncreate(IsMobileNumPhone.isUid(), strIds, tvgetInfo
					.getText().toString(), editextNote.getText().toString());// //(beans);

			break;
		case R.id.back:
			this.finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			System.out.println("beansbeanwww");
			if (entity.getEventType() == presenter.Creat_success) {
				toast("" + entity.getData());
				finish();
			}
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		if (tvgetInfo.getText().toString().length() > 0
				|| editextNote.getText().toString().length() > 0) {
			btnSendOrder.setEnabled(true);
			btnSendOrder.setTextColor(getResources().getColor(R.color.white));
			btnSendOrder.setBackground(getResources().getDrawable(
					R.drawable.btn_ok_order));
		} else {
			btnSendOrder.setBackground(getResources().getDrawable(
					R.drawable.btn_oj_bac));
			btnSendOrder.setTextColor(getResources().getColor(R.color.gray));
			btnSendOrder.setEnabled(false);
		}
	}

}
